package com.ruoyi.system.FabricBussinessContract;

import com.ruoyi.system.BusinessContract.domain.BusinessContract;
import org.springframework.beans.BeanUtils;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ContractFabricToBussinessMapper {

    private static final String DATE_FORMAT = "yyyy-MM-dd";

    // 将 BusinessContract 转换为 BussinessContractDTO
    public static BussinessContractDTO toDTO(BusinessContract businessContract) {
        BussinessContractDTO dto = new BussinessContractDTO();
        BeanUtils.copyProperties(businessContract, dto);

        // 转换 Date 类型字段为 String 类型
        if (businessContract.getSignDate() != null) {
            dto.setSignDate(formatDate(businessContract.getSignDate()));
        }
        if (businessContract.getEffectDate() != null) {
            dto.setEffectDate(formatDate(businessContract.getEffectDate()));
        }
        if (businessContract.getExpirationDate() != null) {
            dto.setExpirationDate(formatDate(businessContract.getExpirationDate()));
        }
        if (businessContract.getAbolishDate() != null) {
            dto.setAbolishDate(formatDate(businessContract.getAbolishDate()));
        }

        // 其他字段按需转换
        return dto;
    }

    // 将 BussinessContractDTO 转换为 BusinessContract
    public static BusinessContract toBusinessContract(BussinessContractDTO dto) {
        BusinessContract businessContract = new BusinessContract();
        BeanUtils.copyProperties(dto, businessContract);

        // 转换 String 类型的日期到 Date
        try {
            if (dto.getSignDate() != null) {
                businessContract.setSignDate(parseDate(dto.getSignDate()));
            }
            if (dto.getEffectDate() != null) {
                businessContract.setEffectDate(parseDate(dto.getEffectDate()));
            }
            if (dto.getExpirationDate() != null) {
                businessContract.setExpirationDate(parseDate(dto.getExpirationDate()));
            }
            if (dto.getAbolishDate() != null) {
                businessContract.setAbolishDate(parseDate(dto.getAbolishDate()));
            }
        } catch (Exception e) {
            // Handle date parsing exception if necessary
            e.printStackTrace();
        }

        // 其他字段按需转换
        return businessContract;
    }

    // 将 Date 转换为指定格式的字符串
    private static String formatDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        return sdf.format(date);
    }

    // 将字符串解析为 Date
    private static Date parseDate(String dateStr) {
        if (dateStr == null || dateStr.trim().isEmpty()) {
            return null;
        }
        try {
            String normalizedDateStr = dateStr.trim().replace("/", "-");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            java.util.Date utilDate = sdf.parse(normalizedDateStr);
            return new Date(utilDate.getTime());
        } catch (Exception e) {
            System.err.println("日期格式错误: " + dateStr);
            return null;
        }
    }
}
